package com.gisocn.onemap.ui.fragment;

import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import com.gisocn.onemap.base.BaseFragment;
import com.gisocn.onemap.base.BasePresenter;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.event.MessageEvent;
import com.gisocn.onemap.interfaces.OnChildItemCheckListener;
import com.gisocn.onemap.interfaces.OnChildSeekbarCheckListener;
import com.gisocn.onemap.model.AppConfig;
import com.gisocn.onemap.model.DynamicMapServiceLayer;
import com.gisocn.onemap.model.GroupLayer;
import com.gisocn.onemap.model.LayerInfoBean;
import com.gisocn.onemap.model.Subcatalog;
import com.gisocn.onemap.model.TreePoint;
import com.gisocn.onemap.sgzx.R;
import com.gisocn.onemap.ui.activity.MainActivity;
import com.gisocn.onemap.ui.adapter.TreeLayerAdapter;
import com.gisocn.onemap.utils.FileUtils;
import com.gisocn.onemap.utils.L;
import com.gisocn.onemap.utils.tree.TreeUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/* loaded from: classes.dex */
public class LayerManageFragment extends BaseFragment {
    private TreeLayerAdapter adapter;
    private AppConfig appConfig;
    private float curPosX;
    private float curPosY;
    private ArrayList<LayerInfoBean> layerList;
    @BindView(R.id.lv_tree)
    ListView lvTree;
    private float posX1;
    private float posY1;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    Unbinder unbinder;
    private ArrayList<String> tpkNames = new ArrayList<>();
    private ArrayList<DynamicMapServiceLayer> dynamicLayers = new ArrayList<>();
    private ArrayList<DynamicMapServiceLayer> wmtsLayers = new ArrayList<>();
    boolean isTpk = false;
    private List<TreePoint> pointList = new ArrayList();
    private HashMap<String, TreePoint> pointMap = new HashMap<>();
    private int whichOne = 0;
    View.OnTouchListener listener_touch_view = new View.OnTouchListener() { // from class: com.gisocn.onemap.ui.fragment.LayerManageFragment.3
        @Override // android.view.View.OnTouchListener
        public boolean onTouch(View view, MotionEvent motionEvent) {
            L.e("滑动setOnTouchListener");
            int action = motionEvent.getAction();
            if (action == 0) {
                LayerManageFragment.this.posX1 = motionEvent.getX();
                LayerManageFragment.this.posY1 = motionEvent.getY();
            } else if (action != 1) {
                if (action == 2) {
                    LayerManageFragment.this.curPosX = motionEvent.getX();
                    LayerManageFragment.this.curPosY = motionEvent.getY();
                }
            } else if (LayerManageFragment.this.curPosX - LayerManageFragment.this.posX1 <= 0.0f || Math.abs(LayerManageFragment.this.curPosX - LayerManageFragment.this.posX1) <= 25.0f) {
                if (LayerManageFragment.this.curPosX - LayerManageFragment.this.posX1 < 0.0f && Math.abs(LayerManageFragment.this.curPosX - LayerManageFragment.this.posX1) > 25.0f) {
                    L.e("向左滑动");
                    LayerManageFragment.this.getActivity().findViewById(R.id.viewPager).setVisibility(8);
                }
            } else {
                L.e("向右滑动");
            }
            return true;
        }
    };
    OnChildItemCheckListener childItemCheckListener = new OnChildItemCheckListener() { // from class: com.gisocn.onemap.ui.fragment.LayerManageFragment.4
        @Override // com.gisocn.onemap.interfaces.OnChildItemCheckListener
        public void onItemClick(View view, int i, boolean z) {
            LayerManageFragment.this.childItemClickMethod(view, i, z);
        }
    };
    OnChildSeekbarCheckListener childSeekbarCheckListener = new OnChildSeekbarCheckListener() { // from class: com.gisocn.onemap.ui.fragment.LayerManageFragment.5
        @Override // com.gisocn.onemap.interfaces.OnChildSeekbarCheckListener
        public void onSeekbarClick(View view, int i, int i2) {
            LayerManageFragment.this.getClickWhichOne(i);
            ArrayList<String> arrayList = new ArrayList<>();
            ArrayList arrayList2 = new ArrayList();
            arrayList2.add(((TreePoint) LayerManageFragment.this.pointList.get(LayerManageFragment.this.whichOne)).getLayer());
            String trim = ((LayerInfoBean) arrayList2.get(0)).getService().trim();
            if (trim.contains("tpk")) {
                arrayList.add(trim);
            } else {
                arrayList.add(((LayerInfoBean) arrayList2.get(0)).getName());
            }
            ((TreePoint) LayerManageFragment.this.pointList.get(LayerManageFragment.this.whichOne)).getLayer().setProgress(i2);
            ((MainActivity) LayerManageFragment.this.getActivity()).setOpacity(arrayList, i2 / 100.0f);
        }
    };

    @Override // com.gisocn.onemap.base.BaseFragment
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override // com.gisocn.onemap.base.BaseFragment
    protected void onInvisible() {
    }

    @Override // com.gisocn.onemap.base.BaseFragment, androidx.fragment.app.Fragment
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
        View inflate = layoutInflater.inflate(R.layout.fragment_layer_manage, viewGroup, false);
        this.unbinder = ButterKnife.bind(this, inflate);
        this.tvTitle.setTypeface(Typeface.defaultFromStyle(1));
        inflate.setOnTouchListener(this.listener_touch_view);
        return inflate;
    }

    @Override // com.gisocn.onemap.base.BaseFragment
    protected void initData() {
        EventBus.getDefault().register(this);
        this.appConfig = ((MainActivity) getActivity()).getAppConfig();
        TreeLayerAdapter treeLayerAdapter = new TreeLayerAdapter(getActivity(), this.pointList, this.pointMap);
        this.adapter = treeLayerAdapter;
        this.lvTree.setAdapter((ListAdapter) treeLayerAdapter);
        this.adapter.setOnChildItemCheckListener(this.childItemCheckListener);
        this.adapter.setOnChildSeekbarCheckListener(this.childSeekbarCheckListener);
        this.lvTree.setOnItemClickListener(new AdapterView.OnItemClickListener() { // from class: com.gisocn.onemap.ui.fragment.LayerManageFragment.1
            @Override // android.widget.AdapterView.OnItemClickListener
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                LayerManageFragment.this.adapter.onItemClick(i);
            }
        });
        getDataFromConfig();
    }

    public void getDataFromConfig() {
        List<TreePoint> list = this.pointList;
        if (list != null && list.size() > 0) {
            this.pointList.clear();
        }
        ArrayList<Subcatalog> subcatalog = this.appConfig.getDynamicLayerList().getCatalog().getSubcatalog();
        if (subcatalog != null && subcatalog.size() > 0) {
            int i = 0;
            for (int i2 = 0; i2 < subcatalog.size(); i2++) {
                for (int i3 = 0; i3 < subcatalog.get(i2).getGroupLayers().size(); i3++) {
                    ArrayList arrayList = new ArrayList();
                    for (int i4 = 0; i4 < subcatalog.get(i2).getGroupLayers().get(i3).getLayer().size(); i4++) {
                        LayerInfoBean layerInfoBean = subcatalog.get(i2).getGroupLayers().get(i3).getLayer().get(i4);
                        boolean isLocalExist = (layerInfoBean.getService() == null || !layerInfoBean.getService().contains("tpk")) ? true : FileUtils.isLocalExist(subcatalog.get(i2).getGroupLayers().get(i3).getLayer().get(i4).getService(), getActivity());
                        arrayList.add(Boolean.valueOf(isLocalExist));
                        subcatalog.get(i2).getGroupLayers().get(i3).getLayer().get(i4).setEnable(isLocalExist);
                        subcatalog.get(i2).getGroupLayers().get(i3).getLayer().get(i4).setProgress(60);
                    }
                    subcatalog.get(i2).getGroupLayers().get(i3).setEnable(arrayList.contains(true));
                }
            }
            int i5 = 1000;
            int i6 = 0;
            int i7 = 0;
            int i8 = 0;
            while (i6 < subcatalog.size()) {
                i5++;
                this.pointList.add(new TreePoint("" + i5, subcatalog.get(i6).getName(), "" + i, "0", i6));
                ArrayList<GroupLayer> groupLayers = subcatalog.get(i6).getGroupLayers();
                if (groupLayers != null && groupLayers.size() > 0) {
                    for (int i9 = 0; i9 < groupLayers.size(); i9++) {
                        if (i9 == 0) {
                            i7 = i5;
                        }
                        i5++;
                        ArrayList<LayerInfoBean> layer = groupLayers.get(i9).getLayer();
                        if (layer != null && layer.size() > 1) {
                            this.pointList.add(new TreePoint("" + i5, groupLayers.get(i9).getName(), "" + i7, "0", i9));
                            for (int i10 = 0; i10 < layer.size(); i10++) {
                                if (i10 == 0) {
                                    i8 = i5;
                                }
                                i5++;
                                TreePoint treePoint = new TreePoint("" + i5, layer.get(i10).getName(), "" + i8, "1", i10);
                                treePoint.setEnable(layer.get(i10).isEnable());
                                treePoint.setOff(layer.get(i10).isOff());
                                treePoint.setSeekBar(layer.get(i10).isSeekBar());
                                treePoint.setIntroduction(layer.get(i10).getIntroduction());
                                treePoint.setService(layer.get(i10).getService());
                                treePoint.setType(layer.get(i10).getType());
                                treePoint.setLayer(layer.get(i10));
                                this.pointList.add(treePoint);
                            }
                        } else {
                            TreePoint treePoint2 = new TreePoint("" + i5, groupLayers.get(i9).getName(), "" + i7, "1", i9);
                            treePoint2.setEnable(groupLayers.get(i9).isEnable());
                            treePoint2.setOff(groupLayers.get(i9).isOff());
                            treePoint2.setSeekBar(groupLayers.get(i9).isSeekBar());
                            treePoint2.setIntroduction(groupLayers.get(i9).getLayer().get(0).getIntroduction());
                            treePoint2.setService(groupLayers.get(i9).getLayer().get(0).getService());
                            treePoint2.setType(groupLayers.get(i9).getLayer().get(0).getType());
                            treePoint2.setLayer(groupLayers.get(i9).getLayer().get(0));
                            this.pointList.add(treePoint2);
                        }
                    }
                }
                i6++;
                i = 0;
            }
        }
        Collections.shuffle(this.pointList);
        updateData();
    }

    private void updateData() {
        for (TreePoint treePoint : this.pointList) {
            this.pointMap.put(treePoint.getID(), treePoint);
        }
        Collections.sort(this.pointList, new Comparator<TreePoint>() { // from class: com.gisocn.onemap.ui.fragment.LayerManageFragment.2
            @Override // java.util.Comparator
            public int compare(TreePoint treePoint2, TreePoint treePoint3) {
                int level = TreeUtils.getLevel(treePoint2, LayerManageFragment.this.pointMap);
                int level2 = TreeUtils.getLevel(treePoint3, LayerManageFragment.this.pointMap);
                if (level == level2) {
                    if (treePoint2.getPARENTID().equals(treePoint3.getPARENTID())) {
                        return treePoint2.getDISPLAY_ORDER() > treePoint3.getDISPLAY_ORDER() ? 1 : -1;
                    }
                    return compare(TreeUtils.getTreePoint(treePoint2.getPARENTID(), LayerManageFragment.this.pointMap), TreeUtils.getTreePoint(treePoint3.getPARENTID(), LayerManageFragment.this.pointMap));
                } else if (level > level2) {
                    if (treePoint2.getPARENTID().equals(treePoint3.getID())) {
                        return 1;
                    }
                    return compare(TreeUtils.getTreePoint(treePoint2.getPARENTID(), LayerManageFragment.this.pointMap), treePoint3);
                } else if (treePoint3.getPARENTID().equals(treePoint2.getID())) {
                    return -1;
                } else {
                    return compare(treePoint2, TreeUtils.getTreePoint(treePoint3.getPARENTID(), LayerManageFragment.this.pointMap));
                }
            }
        });
        this.adapter.notifyDataSetChanged();
    }

    public void getClickWhichOne(int i) {
        TreePoint treePoint = (TreePoint) this.adapter.getItem(i);
        for (int i2 = 0; i2 < this.pointList.size(); i2++) {
            if (treePoint.getID().equals(this.pointList.get(i2).getID())) {
                this.whichOne = i2;
                return;
            }
        }
    }

    public void childItemClickMethod(View view, int i, boolean z) {
        String introduction;
        getClickWhichOne(i);
        if (z && (introduction = this.pointList.get(this.whichOne).getIntroduction()) != null && !introduction.equals("")) {
            ((MainActivity) getActivity()).showIntroduction(introduction, view);
        }
        this.tpkNames.clear();
        this.dynamicLayers.clear();
        this.wmtsLayers.clear();
        this.pointList.get(this.whichOne).setOff(z);
        this.pointList.get(this.whichOne).setSeekBar(z);
        if (this.pointList.get(this.whichOne).getService() != null) {
            this.pointList.get(this.whichOne).getLayer().setOff(z);
            this.pointList.get(this.whichOne).getLayer().setSeekBar(z);
            String trim = this.pointList.get(this.whichOne).getService().trim();
            ArrayList<LayerInfoBean> arrayList = this.layerList;
            if (arrayList != null && arrayList.size() > 0) {
                this.layerList.clear();
            } else {
                this.layerList = new ArrayList<>();
            }
            this.layerList.add(this.pointList.get(this.whichOne).getLayer());
            boolean contains = trim.contains("tpk");
            this.isTpk = contains;
            if (contains) {
                L.e("tpkName_______________" + trim);
                this.tpkNames.add(trim);
            } else {
                String nname = this.pointList.get(this.whichOne).getNNAME();
                String trim2 = this.pointList.get(this.whichOne).getService().trim();
                LayerInfoBean layer = this.pointList.get(this.whichOne).getLayer();
                L.e(" dynamic_______________" + nname);
                if (this.pointList.get(this.whichOne).getType().contains("AgsLayer")) {
                    this.dynamicLayers.add(new DynamicMapServiceLayer(nname, trim2, layer));
                } else if (this.pointList.get(this.whichOne).getType().contains("WMTS")) {
                    this.wmtsLayers.add(new DynamicMapServiceLayer(nname, trim2, layer));
                }
            }
        }
        if (z) {
            ((MainActivity) getActivity()).loadTpkSG(this.layerList);
            ((MainActivity) getActivity()).loadService(this.dynamicLayers);
            ((MainActivity) getActivity()).loadWMTSService(this.wmtsLayers);
            return;
        }
        ((MainActivity) getActivity()).clearTpk(this.tpkNames);
        ((MainActivity) getActivity()).clearService(this.dynamicLayers);
        ((MainActivity) getActivity()).loadWMTSService(this.wmtsLayers);
    }

    @Override // androidx.fragment.app.Fragment
    public void onResume() {
        super.onResume();
        Log.e("onResume", "onResume");
    }

    @Override // com.gisocn.onemap.base.BaseFragment, androidx.fragment.app.Fragment
    public void onDestroyView() {
        super.onDestroyView();
        this.unbinder.unbind();
    }

    @OnClick({R.id.tv_close})
    public void onViewClicked(View view) {
        if (view.getId() != R.id.tv_close) {
            return;
        }
        ((MainActivity) getActivity()).stopTimer();
        getActivity().findViewById(R.id.viewPager).setVisibility(8);
    }

    private void updata(boolean z) {
        for (int i = 0; i < this.pointList.size(); i++) {
            TreePoint treePoint = this.pointList.get(i);
            treePoint.setEnable((treePoint.getService() == null || !treePoint.getService().contains("tpk")) ? true : FileUtils.isLocalExist(treePoint.getService(), getActivity()));
            if (z) {
                treePoint.setOff(false);
                treePoint.setSeekBar(false);
            }
        }
        this.adapter.notifyDataSetChanged();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent messageEvent) {
        if (messageEvent.eventType.equals(Constant.MESSAGE_EVENT_UPDATA_LAYER_DATA)) {
            updata(false);
        }
        if (messageEvent.eventType.equals(Constant.MESSAGE_EVENT_UPDATA_LAYER_DATA_ALL)) {
            updata(true);
        }
    }

    @Override // androidx.fragment.app.Fragment
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
